﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using TiaoTiaoCode.MultiLanguage;

namespace TiaoTiaoCode.DesignLangTranstlateTool.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        #region 字段

        #endregion

        #region 属性

        private string originLang = string.Empty;
        private string jsonLang = string.Empty!;
        private string xamlPath = string.Empty;
        private ObservableCollection<string>? canUseLangs;

        public string XamlPath
        {
            get { return xamlPath; }
            set { SetProperty(ref xamlPath, value); }
        }

        public string OriginLang
        {
            get { return originLang; }
            set { SetProperty(ref originLang, value); }
        }

        public string JsonLang
        {
            get { return jsonLang; }
            set { SetProperty(ref jsonLang, value); }
        }

        public ObservableCollection<string>? CanUseLangs
        {
            get { return canUseLangs; }
            private set
            {
                SetProperty(ref canUseLangs, value);
            }
        }
        private string? selectedlang;

        public string? SelectedLang
        {
            get { return selectedlang; }
            set
            {
                SetProperty(ref selectedlang, value);
                TiaoTiaoMultiLang.SetLang(value);
                var res = Application.Current.Resources.MergedDictionaries[0];
                foreach (var kv in TiaoTiaoMultiLang.CurLangDictionary)
                {
                    if (res.Contains(kv.Key))
                        res[kv.Key] = kv.Value;
                }
            }
        }

        #endregion

        #region 命令

        IRelayCommand? _loadCommand;
        IRelayCommand? _makeCommand;
        IRelayCommand? _saveCommand;

        public IRelayCommand LoadCommand => _loadCommand ??= new RelayCommand<string?>(Load);
        public IRelayCommand MakeCommand => _makeCommand ??= new RelayCommand(Make);
        public IRelayCommand SaveCommand => _saveCommand ??= new RelayCommand<string?>(Save);

        IRelayCommand? _onDropDownOpenedCommand;

        public IRelayCommand OnDropDownOpenedCommand => _onDropDownOpenedCommand ??= new RelayCommand(OnDropDownOpened);

        private void OnDropDownOpened()
        {
            CanUseLangs = new ObservableCollection<string>(TiaoTiaoMultiLang.CanUseLangs);
        }

        #endregion

        public MainViewModel()
        {
            CanUseLangs = new ObservableCollection<string>(TiaoTiaoMultiLang.CanUseLangs);
            SelectedLang = CanUseLangs.First();
        }

        #region 命令实现

        void Load(string? fileName)
        {
            XamlPath = fileName!;

            var txt = File.ReadAllText(fileName!);
            OriginLang = txt;
        }

        void Make()
        {
            var reg = new Regex(".*\"(?<key>.*)\">(?<value>.*)<");
            var lines = OriginLang!.Split("\r\n", System.StringSplitOptions.RemoveEmptyEntries);

            var dic = new Dictionary<string, string>();
            foreach (var line in lines)
            {
                var match = reg.Match(line);
                if (match.Success)
                {
                    var key = match.Groups["key"].Value;
                    var value = match.Groups["value"].Value;

                    if (dic.ContainsKey(key))
                        dic[key] = value;
                    else
                        dic.Add(key, value);
                }
            }

            var str = JsonConvert.SerializeObject(dic, Formatting.Indented);
            JsonLang = str;
        }

        void Save(string? fileName)
        {
            File.WriteAllText(fileName!, JsonLang);

            //TiaoTiaoMultiLang.ChangeAndSaveFile(
            //    XamlPath,
            //    @"D:\01AY\TiaoTiaoCode\小工具\TiaoTiaoCode.DesignLangTranstlateTool\bin\Debug\net6.0-windows\lang\main.json"
            //    );
        }

        #endregion
    }
}
